package services

import (
	"github.com/labstack/gommon/log"
	"law_case_api/dto"
	"law_case_api/models"
)

// CreateCaseCost 创建案件信息
func (_case CaseService) CreateCaseCost(wzjCaseCost models.WzjCaseCost) (bool, error) {
	if err := db.Create(&wzjCaseCost).Error; err != nil {
		log.Error("CreateCaseCost--创建失败,失败原因：", err)
		return false, err
	}
	return true, nil
}

// UpdateCaseCost 更新案件信息 --
func (_case CaseService) UpdateCaseCost(wzjCaseCost models.WzjCaseCost) (bool, error) {
	if err := db.Where("id=?", wzjCaseCost.Id).
		Select("case_id,cost_type,cost_amount,cost_remark,cost_create_date,cost_create_user,cost_create_user_id").
		Updates(&wzjCaseCost).Error; err != nil {
		log.Error("UpdateCaseCost--更新失败,失败原因：", err)
		return false, err
	}
	return true, nil
}

// GetCaseCostById 根据合同id获取合同详情
func (_case CaseService) GetCaseCostById(caseId int) (models.WzjCaseCost, error) {
	wzjCaseCost := models.WzjCaseCost{}
	if err := db.Where("id = ?", caseId).Limit(1).Find(&wzjCaseCost).Error; err != nil {
		log.Error("GetCaseCostById--查询失败,失败原因：", err)
		return wzjCaseCost, err
	}
	return wzjCaseCost, nil
}

// GetCaseCostListByCondition 根据登录人信息获取合同列表
func (_case CaseService) GetCaseCostListByCondition(userId int, caseCostListReq dto.CaseCostListReq) ([]models.WzjCaseCost, int64, error) {
	var wzjCaseCosts []models.WzjCaseCost
	tx := db.Where("case_id=?", caseCostListReq.CaseId)
	if caseCostListReq.CostType != 0 {
		tx.Where("cost_type=?", caseCostListReq.CostType)
	}
	dataCount := int64(0)
	if err := tx.
		Select("id,case_id,cost_type,cost_amount,cost_remark,cost_create_date,cost_create_user,cost_create_user_id").
		Count(&dataCount).
		Limit(int(caseCostListReq.PageSize)).
		Offset(int((caseCostListReq.PageIndex - 1) * caseCostListReq.PageSize)).
		Order("cost_create_date desc").
		Find(&wzjCaseCosts).Error; err != nil {
		log.Error("GetCaseCostListByCreateUserId--查询失败,失败原因：", err)
		return wzjCaseCosts, dataCount, err
	}
	return wzjCaseCosts, dataCount, nil
}
